const Role = require("../model/user/role")
const User = require("../model/user/user")
const Rule = require("../model/user/rule")

const find = async (username) => {
    let isFind = await User.findOne({
        where: { username },
        attributes: ["username", "role", "role_id"],
        include: [{
            model: Role,
            attributes: ["rolename", "rolekey", "creator", "id"],
            include: [{
                model: Rule,
                attributes: ["rulename", "id", "summary"]
            }]
        }]
    })
    return isFind
}

const checkAll = async (rule, name) => {
    console.log(rule, name);
    try {
        let event = await find(name)
        if (event) {
            // 遍历用户权限
            let value = event.Role.Rules.find(item => {
                return item.rulename === rule
            })
            // console.log(value,"event.Role.Rules");
            if (value) return true
            else {
                return false
            }
        } else {
            return false
        }
    } catch (err) { return { msg: "操作失败", code: "400", err } }
}

module.exports = {
    checkAll
}